package com.blb.bookms.dao.impl;

import com.blb.bookms.dao.IBookDAO;
import com.blb.bookms.entity.Book;
import com.blb.bookms.util.DBUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDAOImpl implements IBookDAO {
    /**
     * 查看所有的书籍
     * @return
     */
    @Override
    public List<Book> selectAllBooks() {
        return DBUtils.executeQuery(Book.class,"select * from tb_book");
    }

    @Override
    public int selectCount() {
        return DBUtils.queryCount("select count(*) from tb_book");
    }

    @Override
    public List<Book> selectBooksLimit(int start, int length) {
        return DBUtils.executeQuery(Book.class,"select * from tb_book limit ?,?",start,length);
    }

    @Override
    public int selectCount(int typeId) {
        return DBUtils.queryCount("select count(*) from tb_book where type_id = ?",typeId);
    }

    @Override
    public List<Book> selectBooksLimit(int typeId, int start, int length) {
        return DBUtils.executeQuery(Book.class,"select * from tb_book where type_id=? limit ?,?",typeId,start,length);
    }

    @Override
    public void saveBook(Book book) {
        DBUtils.update("insert into tb_book(book_name,price,type_id,author,publish_org,publish_time,state)" +
                " values(?,?,?,?,?,?,?)",book.getBookName(),book.getPrice(),book.getTypeId(),book.getAuthor(),
                book.getPublishOrg(),book.getPublishTime(),book.getState());
    }
}
